<template>
	<main-container>
		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div>
				<el-button type="primary" v-auth="'upms:sysdept:add'" @click="add">
					<el-icon>
						<ele-Plus />
					</el-icon>
					新增</el-button
				>
			</div>
			<right-toolbar :searchBtn="false" :refreshBtn="true" @search="showSearch = !showSearch" @refresh="initPage"></right-toolbar>
		</div>
		<Form ref="refForm" :type="dialogType" @initPage="initPage"></Form>
		<!-- 列表 -->
		<el-table row-key="id" default-expand-all v-loading="loading" :data="tableData" border>
			<el-table-column prop="name" label="部门名称" />
			<el-table-column prop="leader" label="负责人" />
			<el-table-column prop="leaderPhone" label="负责人手机号" />
			<el-table-column prop="createTime" label="创建时间" />
			<el-table-column prop="sort" label="排序序号" />
			<el-table-column prop="status" label="状态">
				<template #default="scope">
					<dict-tag :options="status" :value="scope.row.status"></dict-tag>
				</template>
			</el-table-column>
			<el-table-column label="操作" width="200" align="center" fixed="right">
				<template #default="scope">
					<el-button link type="primary" v-auth="'upms:sysdept:edit'" @click="edit(scope.row)">
						<el-icon> <ele-Edit /> </el-icon> 修改
					</el-button>
					<el-button link type="danger" v-auth="'upms:sysdept:del'" @click="del(scope.row.id)">
						<el-icon> <ele-Delete /> </el-icon>
						删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>
	</main-container>
</template>
<script lang="ts" setup>
import { defineAsyncComponent, ref, reactive, toRefs } from 'vue';
import { getTreeList, delObj } from '/@/api/upms/sysdept';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useDict } from '/@/utils/dict';
const Form = defineAsyncComponent(() => import('./form.vue'));
// 字典
const { status } = useDict('status');
const state = reactive({
	tableData: [],
});
const showSearch = ref(true);
const loading = ref(false);
const dialogType = ref('add');
const refForm = ref();
const { tableData } = toRefs(state);

const initPage = async () => {
	loading.value = true;
	await getTreeList()
		.then((response: any) => {
			tableData.value = response.data.data;
			loading.value = false;
		})
		.catch(() => {
			loading.value = false;
		});
};
initPage();
/**
 * 新增按钮
 */
const add = () => {
	refForm.value.initForm();
};
/**
 * 修改按钮
 */
const edit = (row: any) => {
	refForm.value.initForm(row);
};
/**
 * 删除按钮
 */
const del = (id: string) => {
	ElMessageBox.confirm('此操作将删除该部门，是否继续?', '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		delObj(id)
			.then(() => {
				ElMessage.success('删除成功');
				initPage();
			})
			.catch(() => {});
	});
};
</script>
